package com.lanhai.zhonglai.impl;

import java.util.ArrayList; 
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanhai.zhonglai.dao.RoadDao; 
import com.lanhai.zhonglai.interfaces.IRoad;
import com.lanhai.zhonglai.mapper.RoadMapper;

@Service
public class RoadImpl implements IRoad {

	@Autowired 
	RoadMapper roadMapper;
	
	
	List<RoadDao> list= new ArrayList<RoadDao>( );
	
	@Override
	public RoadDao getTransRoad(int dot, int transdot) {
		RoadDao p=null;
		for(RoadDao road : list) {
			if(road.getSourcedot()==dot&&road.getTargetTransdot()==transdot) {
				p=new RoadDao();
				p.setId(road.getId());
				p.setSourcedot(road.getSourcedot());
				p.setSourceTransdot(road.getSourceTransdot());
				p.setTargetdot(road.getTargetdot());
				p.setTargetTransdot(road.getTargetTransdot());
				break;
			}
		}
		return p;
	}

	@Override
	public RoadDao getTransRoadFromStation(int sourceTrans, int targetTrans) {
		RoadDao p=null;
		for(RoadDao road : list) {
			if(road.getSourceTransdot()==sourceTrans&&road.getTargetTransdot()==targetTrans) {
				p=new RoadDao();
				p.setId(road.getId());
				p.setSourcedot(road.getSourcedot());
				p.setSourceTransdot(road.getSourceTransdot());
				p.setTargetdot(road.getTargetdot());
				p.setTargetTransdot(road.getTargetTransdot());
				break;
			}
		}
		return p;
	}

	
	//初始化数据
	@Override
	public void init() {
		List<RoadDao> tmp=roadMapper.getRoadList();
		list.clear();
		if(tmp!=null) {
			list.addAll(tmp);
		} 
		
	}

	@Override
	public RoadDao getTransRoadLine(int transdot) {
		RoadDao p=null;
		for(RoadDao road : list) {
			if( road.getTargetTransdot()==transdot) {
				p=new RoadDao();
				p.setId(road.getId());
				p.setSourcedot(road.getSourcedot());
				p.setSourceTransdot(road.getSourceTransdot());
				p.setTargetdot(road.getTargetdot());
				p.setTargetTransdot(road.getTargetTransdot());
				break;
			}
		}
		return p;
	}

	@Override
	public int getNormalRoad(int start, int end) { 
		for(RoadDao road : list) {
			if(road.getSourcedot()==start&&road.getTargetdot()==end) {
				 return road.getId(); 
			}
		}
		return 0;
	}

}
